package br.com.android.fragment;

import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnHoverListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;
import br.com.android.fragment.entities.Issues;
import br.com.android.fragment.entities.Options;
import br.com.android.fragment.util.Teste;

public class Fragment2 extends Fragment {

	public static final String ARG_SECTION_NUMBER = "section_number";
	private Issues issue;
	private int size = 1;
	private Context context;
	private LinearLayout linearLayout;
	private EditText editTextIssues;
	private View rootView;
	private RadioGroup radioGroup;

	@Override
	public void onStop() {
		// TODO Auto-generated method stub
		super.onStop();

		// Toast.makeText(context, "STOP", Toast.LENGTH_SHORT).show();
	}

	@Override
	public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState ) {

		context = container.getContext();

		rootView = inflater.inflate( R.layout.activity_fragment2, container, false );

		linearLayout = (LinearLayout) rootView.findViewById( R.id.generate_linear_layout );
		( (TextView) rootView.findViewById( R.id.text_title ) ).setText( issue.getTitulo() );
		issues( issue );

		return rootView;
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();

		// switch ( issue.getTypeIssues() ) {
		// case CHOICEMULTIPLE:
		// Toast.makeText( context, "Pause Multiplo: ", Toast.LENGTH_SHORT
		// ).show();
		// break;
		// case OPEN:
		// Toast.makeText( context, "Pause Edit: " + editTextIssues.getText(),
		// Toast.LENGTH_SHORT ).show();
		// break;
		// case CHOOSEONE:
		// RadioButton radio = (RadioButton) radioGroup.findViewById(
		// radioGroup.getCheckedRadioButtonId() );
		// if ( radio == null )
		// return;
		// Toast.makeText( context, "Pause Único: " + radio.getText(),
		// Toast.LENGTH_SHORT ).show();
		// break;
		// }

	}

	@Override
	public void onCreate( Bundle savedInstanceState ) {
		super.onCreate( savedInstanceState );
		size = Teste.getExample().get( 0 ).getIssueses().size();
		issue = Teste.getExample().get( 0 ).getIssueses().get( (Integer) getArguments().get( ARG_SECTION_NUMBER ) );
	}

	public int getSize() {
		return size;
	}

	public void issues( Issues issues ) {
		switch ( issues.getTypeIssues() ) {
			case CHOICEMULTIPLE:
				choiceMultiple( issues.getOptions() );
				break;
			case OPEN:
				open();
				break;
			case CHOOSEONE:
				chooseOne( issues.getOptions() );
				break;
		}
	}

	public void choiceMultiple( List<Options> ops ) {
		for ( Options op : ops ) {

			CheckBox checkBox = new CheckBox( context );
			checkBox.setText( op.getTitulo() );
			checkBox.setTag( op );
			checkBox.setId( ops.indexOf( op ) );
			checkBox.setOnClickListener( onClickListener );
			linearLayout.addView( checkBox );
		}
	}

	public void chooseOne( List<Options> ops ) {
		radioGroup = (RadioGroup) linearLayout.findViewById( R.id.radio_options );

		for ( Options op : ops ) {
			RadioButton radioButton = new RadioButton( context );

			radioButton.setText( op.getTitulo() );
			radioButton.setChecked( op.getSelected() == null ? false : op.getSelected() );
			radioButton.setTag( op );
			radioButton.setId( ops.indexOf( op ) );

			radioGroup.addView( radioButton );
		}

		radioGroup.setOnCheckedChangeListener( listener );
		// linearLayout.addView( radioGroup );
	}

	OnClickListener onClickListener = new OnClickListener() {
		@Override
		public void onClick( View v ) {
			Toast.makeText( context, " ON CLICK CHECK", Toast.LENGTH_SHORT ).show();

		}
	};
	private final OnCheckedChangeListener listener = new OnCheckedChangeListener() {
		@Override
		public void onCheckedChanged( RadioGroup group, int checkedId ) {
			Toast.makeText( context, " ON CLICK RADIO ", Toast.LENGTH_SHORT ).show();

		}
	};
	private final OnHoverListener onHoverListener = new OnHoverListener() {

		@Override
		public boolean onHover( View v, MotionEvent event ) {
			Toast.makeText( context, " HOVER EDIT ", Toast.LENGTH_SHORT ).show();
			return false;
		}
	};

	public void open() {
		editTextIssues = new EditText( context );
		editTextIssues.setVisibility( 1 );
		editTextIssues.setOnHoverListener( onHoverListener );
		linearLayout.addView( editTextIssues );
	}

	@Override
	public String toString() {
		return "Fragment2 [issue=" + issue + ", size=" + size + ", context=" + context + ", linearLayout=" + linearLayout + ", editTextIssues=" + editTextIssues + ", rootView=" + rootView + "]";
	}

}
